<?php

class Search_UpdateController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Registry::get('layout')->setLayout('duty');
        
        $activeResource=Search_Model_Search::RESOURCE_NAME;
        
        // seting active resource
        Zend_Registry::set('resource',$activeResource);

        $this->view->headTitle()->append(Zend_Registry::get('locale')->_('Search'));
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->flash_messages = $this->_flashMessenger->getMessages();
        
        ini_set('max_execution_time',0);
    }

    public function indexAction()
    {
        if (!Zend_Registry::get('user')->canManage())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('search'));
        }
        
        $nodeTbl=new Application_Model_NodeDb();
        
        $select=Zend_Registry::get('db')->select()->from('nodes',array('co'=>'count(*)'));
        $st=$select->query();
        $result=$st->fetch();
        
        $limit=100;
        $search=new Search_Model_Search();
        
        for($i=0;$i<ceil($result['co']/$limit);$i++)
        {
            list($nodes,$paginator)=$nodeTbl->getNodesPage($i+1,$limit,true);
            
            if (empty($nodes)) break;

            foreach($nodes as $node)
            {
                $search->index($node->id,
                               $node->url_alias,
                               $node->title,
                               $node->short_text,
                               $node->getKeywords(),
                               $node->getDescription(),
                               $node->language
                               );
            }
        }
        
        $this->_helper->FlashMessenger(Zend_Registry::get('locale')->_('Index was updated'));
        return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('search'));
    }


}

